home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / Common / src / dmutil.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-31  |  22.6 KB  |  690 lines

  1. //-----------------------------------------------------------------------------
  2. // File: DMUtil.cpp
  3. //
  4. // Desc: DirectMusic framework classes for playing DirectMusic segments and
  5. //       DirectMusic scripts. Feel free to use this class as a starting point 
  6. //       for adding extra functionality.
  7. //
  8. // Copyright (c) 1999-2001 Microsoft Corp. All rights reserved.
  9. //-----------------------------------------------------------------------------
  10. #define STRICT
  11. #include <dmusicc.h>
  12. #include <dmusici.h>
  13. #include <dsound.h>
  14. #include <dxerr8.h>
  15. #include "DMUtil.h"
  16. #include "DXUtil.h"
  17.  
  18.  
  19.  
  20.  
  21. //-----------------------------------------------------------------------------
  22. // Name: CMusicManager::CMusicManager()
  23. // Desc: Constructs the class
  24. //-----------------------------------------------------------------------------
  25. CMusicManager::CMusicManager()
  26. {
  27.     m_pLoader       = NULL;
  28.     m_pPerformance  = NULL;
  29.     
  30.     // Initialize COM
  31.     HRESULT hr = CoInitialize(NULL);
  32.     m_bCleanupCOM = SUCCEEDED(hr);
  33. }
  34.  
  35.  
  36.  
  37.  
  38. //-----------------------------------------------------------------------------
  39. // Name: CMusicManager::~CMusicManager()
  40. // Desc: Destroys the class
  41. //-----------------------------------------------------------------------------
  42. CMusicManager::~CMusicManager()
  43. {
  44.     SAFE_RELEASE( m_pLoader ); 
  45.  
  46.     if( m_pPerformance )
  47.     {
  48.         // If there is any music playing, stop it.
  49.         m_pPerformance->Stop( NULL, NULL, 0, 0 );
  50.         m_pPerformance->CloseDown();
  51.  
  52.         SAFE_RELEASE( m_pPerformance );
  53.     }
  54.  
  55.     if( m_bCleanupCOM )
  56.         CoUninitialize();
  57. }
  58.  
  59.  
  60.  
  61.  
  62. //-----------------------------------------------------------------------------
  63. // Name: CMusicManager::Initialize()
  64. // Desc: Inits DirectMusic using a standard audio path
  65. //-----------------------------------------------------------------------------
  66. HRESULT CMusicManager::Initialize( HWND hWnd, DWORD dwPChannels, DWORD dwDefaultPathType )
  67. {
  68.     HRESULT hr;
  69.  
  70.     // Create loader object
  71.     if( FAILED( hr = CoCreateInstance( CLSID_DirectMusicLoader, NULL, CLSCTX_INPROC, 
  72.                                        IID_IDirectMusicLoader8, (void**)&m_pLoader ) ) )
  73.         return DXTRACE_ERR( TEXT("CoCreateInstance"), hr );
  74.  
  75.     // Create performance object
  76.     if( FAILED( hr = CoCreateInstance( CLSID_DirectMusicPerformance, NULL, CLSCTX_INPROC, 
  77.                                        IID_IDirectMusicPerformance8, (void**)&m_pPerformance ) ) )
  78.         return DXTRACE_ERR( TEXT("CoCreateInstance"), hr );
  79.  
  80.     // Initialize the performance with the standard audio path.
  81.     // This initializes both DirectMusic and DirectSound and 
  82.     // sets up the synthesizer. Typcially its easist to use an 
  83.     // audio path for playing music and sound effects.
  84.     if( FAILED( hr = m_pPerformance->InitAudio( NULL, NULL, hWnd, dwDefaultPathType, 
  85.                                                 dwPChannels, DMUS_AUDIOF_ALL, NULL ) ) )
  86.     {
  87.         if( hr == DSERR_NODRIVER )
  88.         {
  89.             DXTRACE( "Warning: No sound card found\n" );
  90.             return hr;
  91.         }
  92.  
  93.         return DXTRACE_ERR( TEXT("InitAudio"), hr );
  94.     }
  95.  
  96.     return S_OK;
  97. }
  98.  
  99.  
  100.  
  101.  
  102. //-----------------------------------------------------------------------------
  103. // Name: CMusicManager::SetSearchDirectory()
  104. // Desc: Sets the search directory.  If not called, the current working
  105. //       directory is used to load content.
  106. //-----------------------------------------------------------------------------
  107. HRESULT CMusicManager::SetSearchDirectory( const TCHAR* strMediaPath )
  108. {
  109.     if( NULL == m_pLoader )
  110.         return E_UNEXPECTED;
  111.  
  112.     // DMusic only takes wide strings
  113.     WCHAR wstrMediaPath[MAX_PATH];
  114.     DXUtil_ConvertGenericStringToWide( wstrMediaPath, strMediaPath );
  115.  
  116.     return m_pLoader->SetSearchDirectory( GUID_DirectMusicAllTypes, 
  117.                                           wstrMediaPath, FALSE );
  118. }
  119.  
  120.  
  121.  
  122.  
  123. //-----------------------------------------------------------------------------
  124. // Name: CMusicManager::GetDefaultAudioPath()
  125. // Desc: 
  126. //-----------------------------------------------------------------------------
  127. IDirectMusicAudioPath8* CMusicManager::GetDefaultAudioPath()
  128. {
  129.     IDirectMusicAudioPath8* pAudioPath = NULL;
  130.     if( NULL == m_pPerformance )
  131.         return NULL;
  132.  
  133.     m_pPerformance->GetDefaultAudioPath( &pAudioPath );
  134.     return pAudioPath;
  135. }
  136.  
  137.  
  138.  
  139.  
  140. //-----------------------------------------------------------------------------
  141. // Name: CMusicManager::CollectGarbage()
  142. // Desc: Tells the loader to cleanup any garbage from previously 
  143. //       released objects.
  144. //-----------------------------------------------------------------------------
  145. VOID CMusicManager::CollectGarbage()
  146. {
  147.     if( m_pLoader )
  148.         m_pLoader->CollectGarbage();
  149. }
  150.  
  151.  
  152.  
  153.  
  154. //-----------------------------------------------------------------------------
  155. // Name: CMusicManager::CreateSegmentFromFile()
  156. // Desc: 
  157. //-----------------------------------------------------------------------------
  158. HRESULT CMusicManager::CreateSegmentFromFile( CMusicSegment** ppSegment, 
  159.                                               TCHAR* strFileName, 
  160.                                               BOOL bDownloadNow,
  161.                                               BOOL bIsMidiFile )
  162. {
  163.     HRESULT               hr;
  164.     IDirectMusicSegment8* pSegment = NULL;
  165.  
  166.     // DMusic only takes wide strings
  167.     WCHAR wstrFileName[MAX_PATH];
  168.     DXUtil_ConvertGenericStringToWide( wstrFileName, strFileName );
  169.  
  170.     if ( FAILED( hr = m_pLoader->LoadObjectFromFile( CLSID_DirectMusicSegment,
  171.                                                      IID_IDirectMusicSegment8,
  172.                                                      wstrFileName,
  173.                                                      (LPVOID*) &pSegment ) ) )
  174.     {
  175.         if( hr == DMUS_E_LOADER_FAILEDOPEN )
  176.             return hr;
  177.         return DXTRACE_ERR( TEXT("LoadObjectFromFile"), hr );
  178.     }
  179.  
  180.     *ppSegment = new CMusicSegment( m_pPerformance, m_pLoader, pSegment );
  181.     if (!*ppSegment)
  182.         return E_OUTOFMEMORY;
  183.  
  184.     if( bIsMidiFile )
  185.     {
  186.         if( FAILED( hr = pSegment->SetParam( GUID_StandardMIDIFile, 
  187.                                              0xFFFFFFFF, 0, 0, NULL ) ) )
  188.             return DXTRACE_ERR( TEXT("SetParam"), hr );
  189.     }
  190.  
  191.     if( bDownloadNow )
  192.     {
  193.         if( FAILED( hr = (*ppSegment)->Download() ) )
  194.             return DXTRACE_ERR( TEXT("Download"), hr );
  195.     }
  196.  
  197.     return S_OK;
  198. }
  199.  
  200.  
  201.  
  202.  
  203. //-----------------------------------------------------------------------------
  204. // Name: CMusicManager::CreateSegmentFromResource()
  205. // Desc: 
  206. //-----------------------------------------------------------------------------
  207. HRESULT CMusicManager::CreateSegmentFromResource( CMusicSegment** ppSegment, 
  208.                                                   TCHAR* strResource,
  209.                                                   TCHAR* strResourceType,
  210.                                                   BOOL bDownloadNow,
  211.                                                   BOOL bIsMidiFile )
  212. {
  213.     HRESULT               hr;
  214.     IDirectMusicSegment8* pSegment      = NULL;
  215.     HRSRC                 hres          = NULL;
  216.     void*                 pMem          = NULL;
  217.     DWORD                 dwSize        = 0;
  218.     DMUS_OBJECTDESC       objdesc;
  219.  
  220.     // Find the resource
  221.     hres = FindResource( NULL,strResource,strResourceType );
  222.     if( NULL == hres ) 
  223.         return E_FAIL;
  224.  
  225.     // Load the resource
  226.     pMem = (void*)LoadResource( NULL, hres );
  227.     if( NULL == pMem ) 
  228.         return E_FAIL;
  229.  
  230.     // Store the size of the resource
  231.     dwSize = SizeofResource( NULL, hres ); 
  232.     
  233.     // Set up our object description 
  234.     ZeroMemory(&objdesc,sizeof(DMUS_OBJECTDESC));
  235.     objdesc.dwSize = sizeof(DMUS_OBJECTDESC);
  236.     objdesc.dwValidData = DMUS_OBJ_MEMORY | DMUS_OBJ_CLASS;
  237.     objdesc.guidClass = CLSID_DirectMusicSegment;
  238.     objdesc.llMemLength =(LONGLONG)dwSize;
  239.     objdesc.pbMemData = (BYTE*)pMem;
  240.     
  241.     if (FAILED ( hr = m_pLoader->GetObject( &objdesc,
  242.                                             IID_IDirectMusicSegment8,
  243.                                             (void**)&pSegment ) ) )
  244.     {
  245.         if( hr == DMUS_E_LOADER_FAILEDOPEN )
  246.             return hr;
  247.         return DXTRACE_ERR( TEXT("LoadObjectFromFile"), hr );
  248.     }
  249.  
  250.     *ppSegment = new CMusicSegment( m_pPerformance, m_pLoader, pSegment );
  251.     if( NULL == *ppSegment )
  252.         return E_OUTOFMEMORY;
  253.  
  254.     if( bIsMidiFile )
  255.     {
  256.         // Do this to make sure that the default General MIDI set 
  257.         // is connected appropriately to the MIDI file and 
  258.         // all instruments sound correct.                  
  259.         if( FAILED( hr = pSegment->SetParam( GUID_StandardMIDIFile, 
  260.                                              0xFFFFFFFF, 0, 0, NULL ) ) )
  261.             return DXTRACE_ERR( TEXT("SetParam"), hr );
  262.     }
  263.  
  264.     if( bDownloadNow )
  265.     {
  266.         // The segment needs to be download first before playing.  
  267.         // However, some apps may want to wait before calling this 
  268.         // to because the download allocates memory for the 
  269.         // instruments. The more instruments currently downloaded, 
  270.         // the more memory is in use by the synthesizer.
  271.         if( FAILED( hr = (*ppSegment)->Download() ) )
  272.             return DXTRACE_ERR( TEXT("Download"), hr );
  273.     }
  274.  
  275.     return S_OK;
  276. }
  277.  
  278.  
  279.  
  280.  
  281. //-----------------------------------------------------------------------------
  282. // Name: CMusicManager::CreateScriptFromFile()
  283. // Desc: 
  284. //-----------------------------------------------------------------------------
  285. HRESULT CMusicManager::CreateScriptFromFile( CMusicScript** ppScript, 
  286.                                              TCHAR* strFileName )
  287. {
  288.     HRESULT               hr;
  289.     IDirectMusicScript* pScript = NULL;
  290.  
  291.     // DMusic only takes wide strings
  292.     WCHAR wstrFileName[MAX_PATH];
  293.     DXUtil_ConvertGenericStringToWide( wstrFileName, strFileName );
  294.     
  295.     if ( FAILED( hr = m_pLoader->LoadObjectFromFile( CLSID_DirectMusicScript,
  296.                                                      IID_IDirectMusicScript8,
  297.                                                      wstrFileName,
  298.                                                      (LPVOID*) &pScript ) ) )
  299.         return DXTRACE_ERR_NOMSGBOX( TEXT("LoadObjectFromFile"), hr );
  300.  
  301.     if ( FAILED( hr = pScript->Init( m_pPerformance, NULL ) ) )
  302.         return DXTRACE_ERR( TEXT("Init"), hr );
  303.  
  304.     *ppScript = new CMusicScript( m_pPerformance, m_pLoader, pScript );
  305.     if (!*ppScript)
  306.         return E_OUTOFMEMORY;
  307.  
  308.     return hr;
  309. }
  310.  
  311.  
  312.  
  313.  
  314. //-----------------------------------------------------------------------------
  315. // Name: CMusicManager::CreateChordMapFromFile()
  316. // Desc: 
  317. //-----------------------------------------------------------------------------
  318. HRESULT CMusicManager::CreateChordMapFromFile( IDirectMusicChordMap8** ppChordMap, 
  319.                                                TCHAR* strFileName )
  320. {
  321.     // DMusic only takes wide strings
  322.     WCHAR wstrFileName[MAX_PATH];
  323.     DXUtil_ConvertGenericStringToWide( wstrFileName, strFileName );
  324.  
  325.     return m_pLoader->LoadObjectFromFile( CLSID_DirectMusicChordMap,
  326.                                           IID_IDirectMusicChordMap8,
  327.                                           wstrFileName, (LPVOID*) ppChordMap );
  328. }
  329.  
  330.  
  331.  
  332.  
  333. //-----------------------------------------------------------------------------
  334. // Name: CMusicManager::CreateChordMapFromFile()
  335. // Desc: 
  336. //-----------------------------------------------------------------------------
  337. HRESULT CMusicManager::CreateStyleFromFile( IDirectMusicStyle8** ppStyle, 
  338.                                             TCHAR* strFileName )
  339. {
  340.     // DMusic only takes wide strings
  341.     WCHAR wstrFileName[MAX_PATH];
  342.     DXUtil_ConvertGenericStringToWide( wstrFileName, strFileName );
  343.  
  344.     return m_pLoader->LoadObjectFromFile( CLSID_DirectMusicStyle,
  345.                                           IID_IDirectMusicStyle8,
  346.                                           wstrFileName, (LPVOID*) ppStyle );
  347. }
  348.  
  349.  
  350.  
  351.  
  352. //-----------------------------------------------------------------------------
  353. // Name: CMusicManager::GetMotifFromStyle()
  354. // Desc: 
  355. //-----------------------------------------------------------------------------
  356. HRESULT CMusicManager::GetMotifFromStyle( IDirectMusicSegment8** ppMotif8, 
  357.                                           TCHAR* strStyle, TCHAR* strMotif )
  358. {       
  359.     HRESULT              hr;
  360.     IDirectMusicStyle8*  pStyle = NULL;
  361.     IDirectMusicSegment* pMotif = NULL;
  362.  
  363.     if( FAILED( hr = CreateStyleFromFile( &pStyle, strStyle ) ) )
  364.         return DXTRACE_ERR( TEXT("CreateStyleFromFile"), hr );
  365.  
  366.     if( pStyle )
  367.     {
  368.         // DMusic only takes wide strings
  369.         WCHAR wstrMotif[MAX_PATH];
  370.         DXUtil_ConvertGenericStringToWide( wstrMotif, strMotif );
  371.  
  372.         hr = pStyle->GetMotif( wstrMotif, &pMotif );
  373.         SAFE_RELEASE( pStyle );
  374.  
  375.         if( FAILED( hr ) )
  376.             return DXTRACE_ERR( TEXT("GetMotif"), hr );
  377.  
  378.         pMotif->QueryInterface( IID_IDirectMusicSegment8, (LPVOID*) ppMotif8 );
  379.     }
  380.  
  381.     return S_OK;
  382. }
  383.  
  384.  
  385.  
  386.  
  387. //-----------------------------------------------------------------------------
  388. // Name: CMusicSegment::CMusicSegment()
  389. // Desc: Constructs the class
  390. //-----------------------------------------------------------------------------
  391. CMusicSegment::CMusicSegment( IDirectMusicPerformance8* pPerformance, 
  392.                               IDirectMusicLoader8*      pLoader,
  393.                               IDirectMusicSegment8*     pSegment )
  394. {
  395.     m_pPerformance          = pPerformance;
  396.     m_pLoader               = pLoader;
  397.     m_pSegment              = pSegment;
  398.     m_pEmbeddedAudioPath    = NULL;
  399.     m_bDownloaded           = FALSE;
  400.     
  401.     // Try to pull out an audio path from the segment itself if there is one.
  402.     // This embedded audio path will be used instead of the default
  403.     // audio path if the app doesn't wish to use an overriding audio path.
  404.     IUnknown* pConfig = NULL;
  405.     if( SUCCEEDED( m_pSegment->GetAudioPathConfig( &pConfig ) ) )
  406.     {
  407.         m_pPerformance->CreateAudioPath( pConfig, TRUE, &m_pEmbeddedAudioPath );
  408.         SAFE_RELEASE( pConfig );
  409.     } 
  410.  
  411. }
  412.  
  413.  
  414.  
  415.  
  416. //-----------------------------------------------------------------------------
  417. // Name: CMusicSegment::~CMusicSegment()
  418. // Desc: Destroys the class
  419. //-----------------------------------------------------------------------------
  420. CMusicSegment::~CMusicSegment()
  421. {
  422.     if( m_pSegment )
  423.     {
  424.         // Tell the loader that this object should now be released
  425.         if( m_pLoader )
  426.             m_pLoader->ReleaseObjectByUnknown( m_pSegment );
  427.  
  428.         if( m_bDownloaded )
  429.         {
  430.             if( m_pEmbeddedAudioPath )
  431.                 m_pSegment->Unload( m_pEmbeddedAudioPath );
  432.             else
  433.                 m_pSegment->Unload( m_pPerformance );
  434.         }
  435.  
  436.         SAFE_RELEASE( m_pEmbeddedAudioPath ); 
  437.         SAFE_RELEASE( m_pSegment ); 
  438.     }
  439.  
  440.     m_pPerformance = NULL;
  441. }
  442.  
  443.  
  444.  
  445.  
  446. //-----------------------------------------------------------------------------
  447. // Name: CMusicSegment::Play()
  448. // Desc: Plays the sound using voice management flags.  Pass in DSBPLAY_LOOPING
  449. //       in the dwFlags to loop the sound
  450. //-----------------------------------------------------------------------------
  451. HRESULT CMusicSegment::Play( DWORD dwFlags, IDirectMusicAudioPath8* pAudioPath )
  452. {
  453.     if( m_pSegment == NULL || m_pPerformance == NULL )
  454.         return CO_E_NOTINITIALIZED;
  455.  
  456.     if( !m_bDownloaded )
  457.         return E_FAIL;
  458.  
  459.     // If an audio path was passed in then use it, otherwise
  460.     // use the embedded audio path if there was one.
  461.     if( pAudioPath == NULL && m_pEmbeddedAudioPath != NULL )
  462.         pAudioPath = m_pEmbeddedAudioPath;
  463.         
  464.     // If pAudioPath is NULL then this plays on the default audio path.
  465.     return m_pPerformance->PlaySegmentEx( m_pSegment, 0, NULL, dwFlags, 
  466.                                           0, 0, NULL, pAudioPath );
  467. }
  468.  
  469.  
  470.  
  471.  
  472.  
  473. //-----------------------------------------------------------------------------
  474. // Name: CMusicSegment::Download()
  475. // Desc: 
  476. //-----------------------------------------------------------------------------
  477. HRESULT CMusicSegment::Download( IDirectMusicAudioPath8* pAudioPath )
  478. {
  479.     HRESULT hr;
  480.     
  481.     if( m_pSegment == NULL )
  482.         return CO_E_NOTINITIALIZED;
  483.  
  484.     // If no audio path was passed in, then download
  485.     // to the embedded audio path if it exists 
  486.     // else download to the performance
  487.     if( pAudioPath == NULL )
  488.     {
  489.         if( m_pEmbeddedAudioPath )
  490.             hr = m_pSegment->Download( m_pEmbeddedAudioPath );
  491.         else    
  492.             hr = m_pSegment->Download( m_pPerformance );
  493.     }
  494.     else
  495.     {
  496.         hr = m_pSegment->Download( pAudioPath );
  497.     }
  498.     
  499.     if ( SUCCEEDED( hr ) )
  500.         m_bDownloaded = TRUE;
  501.         
  502.     return hr;
  503. }
  504.  
  505.  
  506.  
  507.  
  508. //-----------------------------------------------------------------------------
  509. // Name: CMusicSegment::Unload()
  510. // Desc: 
  511. //-----------------------------------------------------------------------------
  512. HRESULT CMusicSegment::Unload( IDirectMusicAudioPath8* pAudioPath )
  513. {
  514.     HRESULT hr;
  515.     
  516.     if( m_pSegment == NULL )
  517.         return CO_E_NOTINITIALIZED;
  518.  
  519.     // If no audio path was passed in, then unload 
  520.     // from the embedded audio path if it exists 
  521.     // else unload from the performance
  522.     if( pAudioPath == NULL )
  523.     {
  524.         if( m_pEmbeddedAudioPath )
  525.             hr = m_pSegment->Unload( m_pEmbeddedAudioPath );
  526.         else    
  527.             hr = m_pSegment->Unload( m_pPerformance );
  528.     }
  529.     else
  530.     {
  531.         hr = m_pSegment->Unload( pAudioPath );
  532.     }
  533.         
  534.     if ( SUCCEEDED( hr ) )
  535.         m_bDownloaded = FALSE;
  536.  
  537.     return hr;
  538. }
  539.  
  540.  
  541.  
  542.  
  543. //-----------------------------------------------------------------------------
  544. // Name: CMusicSegment::IsPlaying()
  545. // Desc: 
  546. //-----------------------------------------------------------------------------
  547. BOOL CMusicSegment::IsPlaying()
  548. {
  549.     if( m_pSegment == NULL || m_pPerformance == NULL )
  550.         return CO_E_NOTINITIALIZED;
  551.  
  552.     return ( m_pPerformance->IsPlaying( m_pSegment, NULL ) == S_OK );
  553. }
  554.  
  555.  
  556.  
  557.  
  558. //-----------------------------------------------------------------------------
  559. // Name: CMusicSegment::Stop()
  560. // Desc: Stops the sound from playing
  561. //-----------------------------------------------------------------------------
  562. HRESULT CMusicSegment::Stop( DWORD dwFlags )
  563. {
  564.     if( m_pSegment == NULL || m_pPerformance == NULL )
  565.         return CO_E_NOTINITIALIZED;
  566.  
  567.     return m_pPerformance->Stop( m_pSegment, NULL, 0, dwFlags );;
  568. }
  569.  
  570.  
  571.  
  572.  
  573. //-----------------------------------------------------------------------------
  574. // Name: CMusicSegment::SetRepeats()
  575. // Desc: 
  576. //-----------------------------------------------------------------------------
  577. HRESULT CMusicSegment::SetRepeats( DWORD dwRepeats )
  578. {
  579.     if( m_pSegment == NULL )
  580.         return CO_E_NOTINITIALIZED;
  581.  
  582.     return m_pSegment->SetRepeats( dwRepeats );
  583. }
  584.  
  585.  
  586.  
  587.  
  588. //-----------------------------------------------------------------------------
  589. // Name: CMusicSegment::GetStyle()
  590. // Desc: 
  591. //-----------------------------------------------------------------------------
  592. HRESULT CMusicSegment::GetStyle( IDirectMusicStyle8** ppStyle, DWORD dwStyleIndex )
  593. {
  594.     // Get the Style from the Segment by calling the Segment's GetData() with
  595.     // the data type GUID_StyleTrackStyle. 0xffffffff indicates to look at
  596.     // tracks in all TrackGroups in the segment. The first 0 indicates to
  597.     // retrieve the Style from the first Track  in the indicated TrackGroup.
  598.     // The second 0 indicates to retrieve the Style from the beginning of the
  599.     // segment, i.e. time 0 in Segment time. If this Segment was loaded from a
  600.     // section file, there is only one Style and it is at time 0.
  601.     return m_pSegment->GetParam( GUID_IDirectMusicStyle, 0xffffffff, dwStyleIndex, 
  602.                                  0, NULL, (VOID*)ppStyle );
  603. }
  604.  
  605.  
  606.  
  607. //-----------------------------------------------------------------------------
  608. // Name: CMusicScript::CMusicScript()
  609. // Desc: Constructs the class
  610. //-----------------------------------------------------------------------------
  611. CMusicScript::CMusicScript( IDirectMusicPerformance8* pPerformance, 
  612.                             IDirectMusicLoader8* pLoader,                   
  613.                             IDirectMusicScript8* pScript )
  614. {
  615.     m_pPerformance = pPerformance;
  616.     m_pLoader      = pLoader;
  617.     m_pScript      = pScript;
  618. }
  619.  
  620.  
  621.  
  622.  
  623. //-----------------------------------------------------------------------------
  624. // Name: CMusicScript::~CMusicScript()
  625. // Desc: Destroys the class
  626. //-----------------------------------------------------------------------------
  627. CMusicScript::~CMusicScript()
  628. {
  629.     if( m_pLoader )
  630.     {
  631.         // Tell the loader that this object should now be released
  632.         m_pLoader->ReleaseObjectByUnknown( m_pScript );
  633.         m_pLoader = NULL;
  634.     }
  635.  
  636.     SAFE_RELEASE( m_pScript ); 
  637.     m_pPerformance = NULL;
  638. }
  639.  
  640.  
  641.  
  642.  
  643. //-----------------------------------------------------------------------------
  644. // Name: CMusicScript::Play()
  645. // Desc: Calls a routine in the script
  646. //-----------------------------------------------------------------------------
  647. HRESULT CMusicScript::CallRoutine( TCHAR* strRoutine )
  648. {
  649.     // DMusic only takes wide strings
  650.     WCHAR wstrRoutine[MAX_PATH];
  651.     DXUtil_ConvertGenericStringToWide( wstrRoutine, strRoutine );
  652.  
  653.     return m_pScript->CallRoutine( wstrRoutine, NULL );
  654. }
  655.  
  656.  
  657.  
  658.  
  659. //-----------------------------------------------------------------------------
  660. // Name: CMusicScript::SetVariableNumber()
  661. // Desc: Sets the value of a variable in the script
  662. //-----------------------------------------------------------------------------
  663. HRESULT CMusicScript::SetVariableNumber( TCHAR* strVariable, LONG lValue )
  664. {
  665.     // DMusic only takes wide strings
  666.     WCHAR wstrVariable[MAX_PATH];
  667.     DXUtil_ConvertGenericStringToWide( wstrVariable, strVariable );
  668.  
  669.     return m_pScript->SetVariableNumber( wstrVariable, lValue, NULL );
  670. }
  671.  
  672.  
  673.  
  674.  
  675. //-----------------------------------------------------------------------------
  676. // Name: CMusicScript::GetVariableNumber()
  677. // Desc: Gets the value of a variable in the script
  678. //-----------------------------------------------------------------------------
  679. HRESULT CMusicScript::GetVariableNumber( TCHAR* strVariable, LONG* plValue )
  680. {
  681.     // DMusic only takes wide strings
  682.     WCHAR wstrVariable[MAX_PATH];
  683.     DXUtil_ConvertGenericStringToWide( wstrVariable, strVariable );
  684.  
  685.     return m_pScript->GetVariableNumber( wstrVariable, plValue, NULL );
  686. }
  687.  
  688.  
  689.  
  690.